﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
using VMS.FTS.APPPUSH.LOGGER;
using VMS.FTS.APPPUSH.DEVICE;

namespace VMS.FTS.APPPUSH.DATABASE
{
    public class MemoDatabase :ILogable 
    {
        #region | Singleton construction |
        private static readonly Lazy<MemoDatabase> lazy =
            new Lazy<MemoDatabase>(() => new MemoDatabase());

        public static MemoDatabase Instance { get { return lazy.Value; } }
        #endregion

        #region | Contructor |
        /// <summary>
        /// Default constructor
        /// </summary>
        public MemoDatabase()
        {
            _DATABASE_NAME = @"";
            LOG_TAG = "MemoDatabase";
        }

        // Database name
        private string _DATABASE_NAME = @"";
        public string DATABASE_NAME
        {
            get { return _DATABASE_NAME; }
            set
            {
                _DATABASE_NAME = value;
                // Create database if not exists
                if (!File.Exists(_DATABASE_NAME))
                {
                    _DATABASE_NAME = "";
                    return;
                }

                try
                {
                    // Load database from file.
                    ///rootElement = XElement.Load(DATABASE_NAME);
                    FileStream fs = new FileStream(DATABASE_NAME, FileMode.Open);
                    // Serialize the order to a file.
                    XmlSerializer serializer = new XmlSerializer(typeof(MemoDatamodel));
                    MemoDatamodel catalog = (MemoDatamodel)serializer.Deserialize(fs);

                    foreach (VMSDevice dev in catalog.Devices)
                        Console.Out.WriteLine(dev);

                    foreach (VMSImage img in catalog.Images)
                        Console.Out.WriteLine(img.ToString());
                    ///serializer.Serialize(Console.Out, catalog);
                                                                 
                }
                catch (Exception e)
                {
                    _DATABASE_NAME = "";
                    Log(e.Message , LOG_LEVEL.ERROR);
                    Log(e.InnerException.Message, LOG_LEVEL.ERROR);
                    return;
                }
            }
        }
        #endregion

        #region | Methods |
        
        #endregion

        #region | Fields |
        private XElement rootElement;
        #endregion   
    }


    [XmlRoot("LedCenter")]
    public class MemoDatamodel
    {
        [XmlArray("VMSDevices")]
        [XmlArrayItem("VMSDevice")]
        public LETIDevice[] Devices;

        [XmlArray("VMSImages")]
        [XmlArrayItem("VMSIMage")]
        public VMSImage[] Images;
    }

    // Base class for VMS devices
    public class VMSDeviceXml
    {
        //[XmlElement("Name")]
        ///public VMSDeviceStatus Status { get; set; } // property declartion
        [XmlAttributeAttribute(AttributeName = "ID")]
        public string ID;

        [XmlAttributeAttribute(AttributeName = "IMEI", DataType = "integer")]
        public string IMEI;

        
        [XmlElement("Info")]
        public VMSDeviceInfo Info;/// { get; set; }  // property declartion

        /* 
         * Constructors 
        **/
        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <returns></returns>
        public VMSDeviceXml()
        {
            //Info = new VMSDeviceInfo();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="stt">Device's status</param>
        /// <param name="info">Device's info</param>
        public VMSDeviceXml(VMSDeviceStatus stt, VMSDeviceInfo info)
        {
            //Info = info;
        }
    }

    public class VMSDeviceInfoXml
    {
        // VMS Name
        [XmlElement("Name")]
        public string Name;// { get; set; }
        // VMS Longtitude
        [XmlElement("Longitude")]
        public string Longitude;// { get; set; }
        // VMS Latitude
        [XmlElement("Latitude")]
        public string Latitude;// { get; set; }
        // VMS Type
        [XmlElement("Type")]
        public string Type;// { get; set; }
        // VMS Firmware version
        [XmlElement("Firmware", DataType = "integer")]
        public string FirmwareVersion ;//{ get; set; }
    }
}
